package com.lu.stream;

import jdk.nashorn.internal.ir.CallNode;
import org.junit.Test;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.function.IntConsumer;
import java.util.stream.IntStream;
import java.util.stream.Stream;

/**
 * @Date:2021/11/22/19:53
 * @Description: Stream api 使用案例
 */
public class StreamApi {

    /**
    * @Description: 从集合创建流
    * @Param: []
    * @return: void
    * @Date: 2021/11/25
    */
    @Test
    public void test(){
        List<Employee> employeeList = getEmployeeList();

        Stream<Employee> stream = employeeList.stream();
        stream.forEach(System.out::print);

        // 并行流，拿到为无序
        Stream<Employee> employeeStream = employeeList.parallelStream();
        employeeStream.forEach(System.out::print);
    }

    /**
    * @Description: 通过数组创建流
    * @Param: []
    * @return: void
    * @Date: 2021/11/25
    */
    @Test
   public void test02(){
        int[] arr = {1, 23, 4, 5, 6, 7};
        IntStream stream = Arrays.stream(arr);
        stream.forEach( new IntConsumer(){
            @Override
            public void accept(int value) {
                System.out.println(value);
            }
        } );

        stream.forEach(x->System.out.println(x)); // Lambda表达式

        stream.forEach(System.out::println); // 方法引用
    }

    /**
    * @Description: 通过 Stream.of(),创建Stream
    * @Param: []
    * @return: void
    * @Date: 2021/11/25
    */
    public void test03(){
        Stream<Integer> integerStream = Stream.of(1, 2, 3, 4, 7);
        integerStream.forEach(System.out::print);
    }

    /**
     * @Description: 创建无限流
     * @Param: []
     * @return: void
     * @Date: 2021/11/25
     */
    @Test
    public void test04(){
        // 迭代
        // public static<T> Stream<T> iterate(final T seed, final UnaryOperator<T> f)
        Stream.iterate(0,t->t+2).limit(10).forEach(System.out::println);

        // 生成
        //public static<T> Stream<T> generate(Supplier<T> s)
        Stream.generate(Math::random).limit(10).forEach(System.out::println);
    }


    /**
    * @Description: 获取 list对象
    * @Param: []
    * @return: java.util.List<com.lu.stream.StreamApi.Employee>
    * @Date: 2021/11/25
    */
    public List<Employee> getEmployeeList(){
        List<Employee> list = new ArrayList<>();
        list.add(new Employee(1001, "老马", 34, 4000.8));
        list.add(new Employee(1002, "老陆", 54, 49000.8));
        list.add(new Employee(1003, "老黄", 39, 7300.8));
        list.add(new Employee(1004, "老刘", 58, 6000.8));
        return list;
    }

    /**
    * @Description: bean对象
    * @Param:
    * @return:
    * @Date: 2021/11/25
    */
    public static class Employee{
        int id;
        String name;
        int age;
        Double salary;

        public Employee(int id, String name, int age, Double salary) {
            this.id = id;
            this.name = name;
            this.age = age;
            this.salary = salary;
        }

        /**
        * @Description: 方便打印，否则打印地址
        * @Param: []
        * @return: java.lang.String
        * @Date: 2021/11/25
        */
        @Override
        public String toString() {
            return "Employee{" +
                    "id=" + id +
                    ", name='" + name + '\'' +
                    ", age=" + age +
                    ", salary=" + salary +
                    '}';
        }
    }
}

